package controller.dao;

import model.*;
import java.util.*;
import javax.persistence.*;

import org.apache.catalina.connector.Response;

public class Dao_Product {
		EntityManager manager;
		
		public boolean save(Product product) {
			try	{
				manager = Factory.factory().createEntityManager();
				manager.getTransaction().begin();		
				manager.merge(product);
				manager.getTransaction().commit();
				manager.close();
				return true;
			}catch (Exception e) {
				return false;
			}
		}
		
		public List<Product> listAll(int inicio, int quant){
			manager = Factory.factory().createEntityManager();
			manager.getTransaction().begin();
			Query query = (Query) manager.createQuery("SELECT p FROM Product p where p.active = 1" );
			@SuppressWarnings("unchecked")
			List<Product> produtos =  query.setMaxResults(quant).setFirstResult(inicio).getResultList();
			manager.close();
			return produtos;
		}
		
		public List<Product> listAllProduct(int inicio, int quant, int catId){
			
			manager = Factory.factory().createEntityManager();
			manager.getTransaction().begin();
			Query query = (Query) manager.createQuery("SELECT p FROM Product p join p.categories cat where cat.id = :category");
			query.setParameter("category", catId);
			@SuppressWarnings("unchecked")
			List<Product> produtos =  query.setMaxResults(quant).setFirstResult(inicio).getResultList();
			manager.close();
			return produtos;
		}
		
		public Long totalProduct(){
			manager = Factory.factory().createEntityManager();
			manager.getTransaction().begin();
			Query query = (Query) manager.createQuery("SELECT count(p) FROM Product p where p.active = 1" );
			Long total = (Long) query.getSingleResult();
			manager.close();
			return total;
		}
		
		public Long totalProduct(int catId){
			manager = Factory.factory().createEntityManager();
			manager.getTransaction().begin();
			Query query = (Query) manager.createQuery("SELECT count(p) FROM Product p join p.categories cat where cat.id = :category and p.active = 1" );
			query.setParameter("category", catId);
			Long total = (Long) query.getSingleResult();
			manager.close();
			return total;
		}
		
		public boolean update(Product product){
			try{
				manager = Factory.factory().createEntityManager();
				manager.getTransaction().begin();
				manager.merge(product);
				manager.getTransaction().commit();
				manager.close();
				return true;
			}catch (Exception e) {
				return false;
			}
		}
		
		public boolean delete(Product product){
			try {
				manager = Factory.factory().createEntityManager();
				manager.getTransaction().begin();
				manager.merge(product);
				manager.getTransaction().commit();
				manager.close();				
				return true;
			} catch (Exception e) {
				return false;
			}
		}
				
		public Product list(int id)	{
			try {
				manager = Factory.factory().createEntityManager();
				manager.getTransaction().begin();	
				Product product = manager.find(Product.class, id);
				manager.close();
				return product;
			} catch (Exception e) {
				return null;				
			}
		}
}
